package com.demo.interview.a1b2c3;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author shenguangyang
 */
public class T02_ReentrantLock {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        char[] aI = "ABCDEF".toCharArray();
        char[] aC = "123456".toCharArray();

        new Thread(() -> {
            lock.lock();
            try {
                for (char c : aI) {
                    System.out.print(c);
                    condition.signal();
                    condition.await();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                condition.signal();
                lock.unlock();
            }
        }).start();

        new Thread(() -> {
            lock.lock();
            try {
                for (char c : aC) {
                    System.out.print(c);
                    condition.signal();
                    condition.await();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                condition.signal();
                lock.unlock();
            }
        }).start();
    }
}
